package com.example.treasureadmin.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.treasureadmin.mapper.OrderMapper;
import com.example.treasureadmin.model.EssayModel;
import com.example.treasureadmin.model.OrderModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.sql.Timestamp;
import java.util.List;

@Service
public class OrderService {
    @Autowired
    private OrderMapper orderMapper;
    //获取订单
    public List<OrderModel> getAll(){
        QueryWrapper<OrderModel> queryWrapper = new QueryWrapper<>();
        return orderMapper.selectList(queryWrapper);
    }
    //查询订单信息（以发货状态查询）
    public List<OrderModel> getStatusOrders(int status){
        QueryWrapper<OrderModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status",status);
        return orderMapper.selectList(queryWrapper);
    }
    //发货指定订单
    public void sendGoods(int id)
    {
        UpdateWrapper<OrderModel> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",id).eq("status",0).set("status",1);
        orderMapper.update(null,updateWrapper);
    }
    //修改订单
    public void changeAddr(int id,String province,String city,String addr,String name,String phone)
    {
        UpdateWrapper<OrderModel> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",id).set("province",province).set("city",city).set("addr",addr).set("name",name).set("phone",phone);
        orderMapper.update(null,updateWrapper);
    }
    //查询订单
    public OrderModel getIdOrders(int id){
        QueryWrapper<OrderModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        return orderMapper.selectOne(queryWrapper);
    }
    //添加订单
    public void addOrder(String goodsName, String goodsImage, String email, String province, String city, String addr, String name, String phone) {
        Date t = new Date(System.currentTimeMillis());
        orderMapper.insert(new OrderModel(-1,goodsName,goodsImage,t,email,province,city,addr,0,name,phone));
    }
    //删除订单
    public String deleteOrder(int id){
        QueryWrapper<OrderModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        OrderModel orderModel = orderMapper.selectOne(queryWrapper);
        if (orderModel == null){
            return null;
        }
        orderMapper.delete(queryWrapper);
        return "200";
    }
}
